// Package custom
// @Author Spume
// @Date 2024-12-24 15:13

package custom

import (
	"net/http"

	"github.com/WeixinCloud/wxcloudrun-wxcomponent/comm/errno"
	"github.com/WeixinCloud/wxcloudrun-wxcomponent/comm/log"
	"github.com/WeixinCloud/wxcloudrun-wxcomponent/comm/wx"
	"github.com/gin-gonic/gin"
)

func getTemplatedRaftListHandler(c *gin.Context) {
	list, err := getTemplatedRaftList()
	if err != nil {
		c.JSON(http.StatusOK, errno.ErrSystemError.WithData(err.Error()))
		return
	}

	c.JSON(http.StatusOK, errno.OK.WithData(gin.H{"total": len(list), "records": list}))
}

type getTemplatedRaftListResp struct {
	DraftList []Draft `json:"draftList" wx:"draft_list"`
}
type Draft struct {
	CreateTime             int           `json:"create_time" wx:"create_time"`
	UserVersion            string        `json:"user_version" wx:"user_version"`
	UserDesc               string        `json:"user_desc" wx:"user_desc"`
	DraftId                int           `json:"draft_id" wx:"draft_id"`
	SourceMiniprogramAppid string        `json:"source_miniprogram_appid" wx:"source_miniprogram_appid"`
	SourceMiniprogram      string        `json:"source_miniprogram" wx:"source_miniprogram"`
	Developer              string        `json:"developer" wx:"developer"`
	CategoryList           []interface{} `json:"category_list" wx:"category_list"`
}

func getTemplatedRaftList() ([]Draft, error) {
	_, body, err := wx.GetWxApiWithComponentToken("/wxa/gettemplatedraftlist", "")
	if err != nil {
		log.Error(err)
		return nil, err
	}
	var resp = getTemplatedRaftListResp{
		DraftList: make([]Draft, 0),
	}

	if err = wx.WxJson.Unmarshal(body, &resp); err != nil {
		log.Errorf("Unmarshal err, %v", err)
		return nil, err
	}
	log.Infof("body : %+v resp: %+v", string(body), resp)
	return resp.DraftList, nil
}

type addToTemplateReq struct {
	DraftId      int `json:"draft_id" wx:"draft_id" validate:"required"`
	TemplateType int `json:"template_type,omitempty" wx:"template_type"`
}

func addToTemplateHandler(c *gin.Context) {
	var req addToTemplateReq
	if err := c.ShouldBindJSON(&req); err != nil {
		log.Error(err.Error())
		c.JSON(http.StatusOK, errno.ErrInvalidParam.WithData(err.Error()))
		return
	}
	err := addToTemplate(req)
	if err != nil {
		c.JSON(http.StatusOK, errno.ErrSystemError.WithData(err.Error()))
		return
	}

	c.JSON(http.StatusOK, errno.OK)
}

func addToTemplate(req addToTemplateReq) (err error) {
	_, _, err = wx.PostWxJsonWithComponentToken("/wxa/addtotemplate", "", req)
	if err != nil {
		log.Error(err)
		return
	}

	return
}

type deleteTemplateReq struct {
	TemplateId int `json:"template_id"  wx:"template_id"`
}

func deleteTemplateHandler(c *gin.Context) {
	var req deleteTemplateReq
	if err := c.ShouldBindJSON(&req); err != nil {
		log.Error(err.Error())
		c.JSON(http.StatusOK, errno.ErrInvalidParam.WithData(err.Error()))
		return
	}
	err := deleteTemplate(req)
	if err != nil {
		c.JSON(http.StatusOK, errno.ErrSystemError.WithData(err.Error()))
		return
	}

	c.JSON(http.StatusOK, errno.OK)
}

func deleteTemplate(req deleteTemplateReq) (err error) {
	_, _, err = wx.PostWxJsonWithComponentToken("/wxa/deletetemplate", "", req)
	if err != nil {
		log.Error(err)
		return
	}

	return
}
